package com.shuoyue.adven.main.bussiness;

import android.content.Context;
import android.graphics.Color;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;
import androidx.viewpager.widget.PagerAdapter;

import com.bumptech.glide.Glide;
import com.shuoyue.adven.R;
import com.shuoyue.adven.entity.Adv;

import java.util.ArrayList;
import java.util.List;

public class MyPagerAdapter extends PagerAdapter {
    Context mContext;
    /**
     * View缓存池，从ViewPager中移除的item将会存到这里面，用来复用
     */
//    private List<View> mViewPoolImg = new ArrayList<>();
//    private List<View> mViewPoolText = new ArrayList<>();

    /**
     * 数据源
     */
    private List<Adv> medias = new ArrayList<>();

    public MyPagerAdapter(Context context, List<Adv> videoBeans) {
        mContext = context;
        if (videoBeans != null)
            this.medias.addAll(videoBeans);
    }

    @Override
    public int getCount() {
        return medias == null || medias.size() == 0 ? 0 : Integer.MAX_VALUE;
    }

    @Override
    public boolean isViewFromObject(@NonNull View view, @NonNull Object o) {
        return view == o;
    }


    @NonNull
    @Override
    public Object instantiateItem(@NonNull ViewGroup container, int position) {
        Adv item = medias.get(position % medias.size());
        if (Adv.TYPE_IMG == item.getType()) {
            View view = LayoutInflater.from(mContext).inflate(R.layout.item_image_pager, container, false);
            ImageView imageView = view.findViewById(R.id.img);

            if (mContext != null) {
                Glide.with(mContext)
                        .load(item.getImgUrl())
                        .error(R.mipmap.img_loading_gray)
                        .placeholder(R.mipmap.img_loading_gray)
                        .into(imageView);
            }
            container.addView(view);
            return view;
        } else {
            View view = LayoutInflater.from(mContext).inflate(R.layout.item_property_text, container, false);
            TextView textView = view.findViewById(R.id.textview);
            textView.setText(item.getFontContent());
            if (item.getFontStyle() == 1) {//1：红底白字 2：蓝底白字 3：绿底黑字 4：黄底黑字
                textView.setBackgroundColor(ContextCompat.getColor(mContext, R.color.red));
                textView.setTextColor(ContextCompat.getColor(mContext, R.color.white));
            }
            if (item.getFontStyle() == 2) {//1：红底白字 2：蓝底白字 3：绿底黑字 4：黄底黑字
                textView.setBackgroundColor(ContextCompat.getColor(mContext, R.color.blue));
                textView.setTextColor(ContextCompat.getColor(mContext, R.color.white));
            }
            if (item.getFontStyle() == 3) {//1：红底白字 2：蓝底白字 3：绿底黑字 4：黄底黑字
                textView.setBackgroundColor(ContextCompat.getColor(mContext, R.color.green));
                textView.setTextColor(ContextCompat.getColor(mContext, R.color.black));
            }
            if (item.getFontStyle() == 4) {//1：红底白字 2：蓝底白字 3：绿底黑字 4：黄底黑字
                textView.setBackgroundColor(ContextCompat.getColor(mContext, R.color.yellow));
                textView.setTextColor(ContextCompat.getColor(mContext, R.color.black));
            }
            if (!TextUtils.isEmpty(item.getTextColor()) && !TextUtils.isEmpty(item.getTextColor())) {
                textView.setBackgroundColor(Color.parseColor("#" + item.getBackGroundColor()));
                textView.setTextColor(Color.parseColor("#" + item.getTextColor()));
            }
            container.addView(view);
            return view;
        }
    }

    @Override
    public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
        View itemView = (View) object;
        container.removeView(itemView);
    }

    public void resetData(List<Adv> newData) {
        if (medias == null) {
            medias = new ArrayList<>();
        }
        medias.clear();
        medias.addAll(newData);
        notifyDataSetChanged();
    }


}
